﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WCFService;

namespace WebForum.ForumPages
{
    public partial class PostPage : System.Web.UI.Page
    {
        public List<String> details { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            int fID = Convert.ToInt32(Request.QueryString["ForumID"]);
            int sfID = Convert.ToInt32(Request.QueryString["SubForumID"]);
            int tID = Convert.ToInt32(Request.QueryString["ThreadID"]);
            details = DataAccessLayer.ThreadDataLayer.GetThreadDetails(fID, sfID, tID);
            details[1] = details[1].Replace("\r\n", "<br />");
            //Label8.Text = details[1];
        }

        protected String GetTitle()
        {
            int fID = Convert.ToInt32(Request.QueryString["ForumID"]);
            int sfID = Convert.ToInt32(Request.QueryString["SubForumID"]);
            int tID = Convert.ToInt32(Request.QueryString["ThreadID"]);
            return DataAccessLayer.ThreadDataLayer.GetTitle(fID, sfID, tID);
        }

        protected String GetBody()
        {
            int fID = Convert.ToInt32(Request.QueryString["ForumID"]);
            int sfID = Convert.ToInt32(Request.QueryString["SubForumID"]);
            int tID = Convert.ToInt32(Request.QueryString["ThreadID"]);
            return DataAccessLayer.ThreadDataLayer.GetBody(fID, sfID, tID);
        }

        protected void ObjectDataSource1_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {
            e.InputParameters["forumID"] = Request.QueryString["ForumID"];
            e.InputParameters["subForumID"] = Request.QueryString["SubForumID"];
            e.InputParameters["threadID"] = Request.QueryString["ThreadID"];
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            AppDomain.CurrentDomain.SetData("SQLServerCompactEditionUnderWebHosting", true);
            LogicForumImp forumManager = new LogicForumImp();
            if (Session["UserName"]==null)
            {
                Label6.ForeColor = System.Drawing.ColorTranslator.FromHtml("#ff0000");
                Label6.Text = "You are not Logged in!";
            }
            else
            if (String.IsNullOrEmpty(TextBox3.Text) || String.IsNullOrEmpty(TextBox4.Text))
            {
                Label6.ForeColor = System.Drawing.ColorTranslator.FromHtml("#ff0000");
                Label6.Text = "One of the fields is empty!";
            }
            else
                if (TextBox3.Text.Length > 50 || TextBox4.Text.Length > 4000)
                {
                    Label6.ForeColor = System.Drawing.ColorTranslator.FromHtml("#ff0000");
                    Label6.Text = "a Title can't be longer than 50 characters and the body can't be longer than 4000 characters";
                }
                else
                    {
                        int flag = forumManager.webAddPost((string)Session["UserName"], (int)Session["Forum"], Convert.ToInt32(Request.QueryString["SubForumID"]), Convert.ToInt32(Request.QueryString["ThreadID"]), TextBox3.Text, TextBox4.Text);
                        switch (flag)
                        {
                            case -1:
                                Label6.ForeColor = System.Drawing.ColorTranslator.FromHtml("#ff0000");
                                Label6.Text = "There was a problem posting your Thread";
                                break;
                            case 2:
                                Label6.ForeColor = System.Drawing.ColorTranslator.FromHtml("#ff0000");
                                Label6.Text = "Your post contains a forbidden word!";
                                break;
                            default:
                                Label6.ForeColor = System.Drawing.ColorTranslator.FromHtml("#000000");
                                Label6.Text = "Thread Posted";
                                Response.Redirect(Request.RawUrl);
                                break;
                        }
                    }
        
        }

    }
}